//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Last Modif: 13/03/2005
//


var MOD_STG={};

MOD_STG.strat_onclickclar=function(cl){
  if(document.getElementById("C"+cl).checked) {
    SetClaimUnclear(cl);
  } else {
    SetClaimClear(cl);
  }
}


MOD_STG.strat_onclicknov=function(cl){
  if(document.getElementById("N"+cl).checked) {
    var dep=GetClaimDep(cl);
    if(is_cldependent(cl)) {
      if(IsClaimNotInventive(dep)) {
	alert("Error: Claim "+cl+" cannot be set Not New because it depends on claim "+dep+" which is not inventive");
	document.getElementById("N"+cl).checked=0;
	return;
      } 
    }
    document.getElementById("I"+cl).checked=0;
    SetClaimNotNovel(cl);
  } else {
    SetClaimNotSearched(cl);
  } 
  MOD_STG.strat_propagate_manual_changes(cl);
}

MOD_STG.strat_onclickinv=function(cl){
  if(document.getElementById("I"+cl).checked) {    
    document.getElementById("N"+cl).checked=0;
    SetClaimNotInventive(cl);
  } else {
    SetClaimNovelandInventive(cl);
  } 
  MOD_STG.strat_propagate_manual_changes(cl);
}

MOD_STG.strat_updatenotsearchedview=function() {
  document.getElementById("noop").value=List_tolist(GetAllClaims("??"));
}


MOD_STG.strat_updateclentrycheckboxes=function(cl) {
  if(IsClaimNotNovel(cl)) {
    document.getElementById("N"+cl).checked=1;
    document.getElementById("I"+cl).checked=0;
  } else if(IsClaimNotInventive(cl)) {
    document.getElementById("N"+cl).checked=0;
    document.getElementById("I"+cl).checked=1;
  } else {
    document.getElementById("N"+cl).checked=0;
    document.getElementById("I"+cl).checked=0;
  } 
  if(GetClaimEq(cl) || IsClaimSearched(cl)) {
    document.getElementById("N"+cl).disabled=1;
    document.getElementById("I"+cl).disabled=1;
  } else {
    document.getElementById("N"+cl).disabled=0;
    document.getElementById("I"+cl).disabled=0;
  }
}

MOD_STG.strat_updateclentryselects=function(cl) {
  var eq=GetClaimEq(cl);
  if(eq) {
    document.getElementById("DX"+cl).disabled=1;
    document.getElementById("DY"+cl).disabled=1;
    document.getElementById("DA"+cl).disabled=1;
    document.getElementById("DB"+cl).disabled=1;
    if(GetPrefsAttr("docC")) {
      document.getElementById("DC"+cl).disabled=1;
    }
    document.getElementById("DH"+cl).disabled=1;
  } else {
    document.getElementById("DX"+cl).disabled=0;
    document.getElementById("DY"+cl).disabled=0;
    document.getElementById("DA"+cl).disabled=0;
    document.getElementById("DB"+cl).disabled=0;
    if(GetPrefsAttr("docC")) {
      document.getElementById("DC"+cl).disabled=0;
    }
    document.getElementById("DH"+cl).disabled=0;

  }
  doc_doselect("DX"+cl,GetClaimDoc_X(cl,eq));
  doc_doselect("DY"+cl,GetClaimDoc_Y(cl,eq));
  doc_doselect("DA"+cl,GetClaimDoc_A(cl,eq));
  doc_doselect("DB"+cl,GetClaimDoc_B(cl,eq));
  if(GetPrefsAttr("docC")) {
    doc_doselect("DC"+cl,GetClaimDoc_C(cl,eq));
  }
  hint_doselect("DH"+cl,GetClaimHint(cl,eq));

  if(GetFeaturesLength(cl,eq)>1) {
    document.getElementById("DH"+cl).disabled=1;
  }
}

MOD_STG.strat_updateclentrygui=function(cl) {
  if(GetDocsLength()>0) {
    //alert("updt selects "+cl);
    MOD_STG.strat_updateclentryselects(cl);
    html_setwarning("eqwarn"+cl,GetClaimEqWarn(cl));
    html_setwarning("grpwarn"+cl,GetClaimGroupWarn(cl));
    //alert("Update warnings "+cl);
  }
  MOD_STG.strat_updateclentrycheckboxes(cl); 
}



MOD_STG.strat_propagate_manual_changes=function(cl) {
  var cl_iter= new claim_iterator(cl);
  while(!cl_iter.end()) {
    updatemanual(cl_iter.getValue());  
    if(IsClaimInFilter(cl_iter.getValue())) {  
      MOD_STG.strat_updateclentrygui(cl_iter.getValue());
    }
    cl_iter.next();
  }  
  MOD_STG.strat_updatenotsearchedview();
}


//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

// Updated to taint
MOD_STG.strat_onchangeEq=function(pcl,eq) {
  new_Taint(pcl);
  changeEq(pcl,eq);
  var cl_iter= new claim_iterator(pcl);
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    if(IsClaimSearched(cl)) {
      updatefromdocs(cl,GetClaimDep(cl));
    } else {
      updatemanual(cl);
    }
    if( IsClaimInFilter(cl) && is_Tainted(cl)) {  
      MOD_STG.strat_updateclentrygui(cl);
    }
    cl_iter.next();
  }
  MOD_STG.strat_updatenotsearchedview();
}


// 
//------------------------------------------------------------------------------

//
// Propose claims to propagate val changes to (dependent) 
//

function prompt_val_downwards_propagation(cl,catsel,seldoc) {
 var children=claim_children(cl);
  if(children) {
    var rep=prompt("Propagate to (only claims on same group)",List_tolist(children));
    if(rep && rep!="") { 
      var ignored= new List();      
      var iter= new List_iterator(list_toList(rep));
      while(!iter.end()) {
        var icl=iter.getValue();
	if(!check_val_integrity(icl,seldoc,catsel))  {
	  ignored.push(icl);
	} else {
           SetClaimDocVal(icl,seldoc,catsel); 
           //updatefromdocs(icl); // no possible manual update 
	} 
	iter.next();	
      }
      if(!ignored.isEmpty()) alert("Warning : could not propagate to claims "+ignored.getcText()+" because of conflicts");    
    }
  }
}


// update val changes to dependent claims 
function fix_val_downwards_propation(cl,pn) {
  // second pass, check branch integrity
  var cl_iter= new claim_iterator(cl);
  while(!cl_iter.end()) {
     var cl2=cl_iter.getValue();
     var dep=GetClaimDep(cl2);     
     var eq=GetClaimEq(cl2);
     if(is_cldependent(cl2)) { // cl2 is a dependent claim, check up            
        if(GetClaimGroup(cl2) == GetClaimGroup(dep)) { // not pseudo-independent
           var depval=GetClaimDocVal(dep,pn,GetClaimEq(dep));
           if(valcmp(GetClaimDocVal(cl2,pn),depval)==">") { // if upper claim is less, fix
		SetClaimDocVal(cl2,pn,depval);                		    
           }      
        }
     } else if(eq) { // independent but equal to someone else
	SetClaimDocVal(cl2,pn,GetClaimDocVal(eq,pn)); // if claim eq, update
     }	        
     if(catvalcmp(GetClaimDocStrat(cl2,pn),GetClaimDocVal(cl2,pn)) == ">") {
       // strategy bigger than value ?, delete strategy
        SetClaimDocStrat(cl2,"",pn);
        updatefromdocs(cl2);
     }
     cl_iter.next();		
  }
}

//
// updated to hints
//
MOD_STG.strat_onchangedoc=function(cat,cl) {
  var seldoc=doc_selected("D"+cat+cl); // DOCPNB2Doc(obj.selectedIndex);
  var clist="";
   
 // explicit change requested 
  var prevtaint=new_Taint();
  //alert("A"+GetClaimDoc_B(cl));
  // Apply 
  switch(cat) {
  case "X":
  case "Y":
  case "A":
    clist=prompt_downwards_propagation(cl);
    break;
  }
  change_stratdoc_claimlist(cl,clist,cat,seldoc);
  
  if(!is_TaintedFrom(prevtaint,cl)) {
    alert("Cannot change strategy for claim "+cl);
    MOD_STG.strat_updateclentrygui(cl);  
    return;
  }

  // Update view    
  
  var cl_iter= new filteredclaims_iterator(cl);  
  while(!cl_iter.end()) {
    var clo=cl_iter.getValue();
    if(is_TaintedFrom(prevtaint,clo)) { 
      // update view of equals  
      MOD_STG.strat_updateclentrygui(clo);
    } else {
      MOD_STG.strat_updateclentrycheckboxes(clo); // not tracked by taints 
    }      
    cl_iter.next();   
  }
  //alert("A2"+GetClaimDoc_B(cl));
  //MOD_STG.strat_updatenotsearchedview();  
  ClaimToolNoOp();
}



MOD_STG.strat_onchangetabSR=function(doc,clist,cat) {
   
  var ignored=new List();

  new_Taint();

  // delete previous values for document doc 
  // restrict to current selection because of groups 
  
  //var iter= new List_iterator(filter_selclaims());   
  var iter = new filteredclaims_iterator();
  while(!iter.end()) {    
    if(GetClaimDocStrat(iter.getValue(),doc)==cat) {
      SetClaimDocStrat(iter.getValue(),"",doc);
    }
    //updatefromdocs(iter.getValue());
    iter.next();
  }  
  change_stratdoc_claimlist("",clist,cat,doc);
  
  edit_strategy();
}


function strat_onupdatecllist() {
  edit_strategy();
}

function strat_cldoclist() {
  var  eqdisabled;
  var  nidisabled;
  var  bdisabled;
  var  cldisabled;


  var html="<TABLE CLASS=CLTAB border=1><TR><TH>Claim</TH>";
  if(GetGroupsLength() >1) { 
    html+="<TH>G</TH>";
  }
  html+="<TH>"+whatis_D()+"</TH>";
  html+="<TH>"+whatis_E()+"</TH>";
  html+="<TH>"+whatis_C()+"</TH>";
  html+="<TH>"+whatis_N()+"</TH>";
  html+="<TH>"+whatis_I()+"</TH>";
  if(GetDocsLength()>0) {
    html+="<TH>X/Y<sub>1</sub></TH><TH>Y<sub>2</sub></TH><TH>A (A<sub>1</sub>)</TH><TH> B (A<sub>2</sub>)</TH>";
    if(GetPrefsAttr("docC")) {
      html+="<TH> C (A<sub>3</sub>)</TH>";
    }
    html+="<TH>Hint</TH>";
  }
  html+="</TR>";
  var cl_iter= new filteredclaims_iterator(); 

  while(!cl_iter.end()) {
    var cl= cl_iter.getValue();
    var dep=GetClaimDep(cl);
    var eq=GetClaimEq(cl);
    var mclass=ClaimTypeStyle(cl);  
    html+="<TR id='clrow"+cl+"'><TH class='"+ mclass+ "'>" + cl + "</TH>";   
    if(GetGroupsLength() >1 ) { 
      html+="<TD>"+group_select(cl)+"</TD>"; 
    }          
    html+="<TD><input type=text name=D size=2 value='" + dep + "' tabindex='" + cl + "' disabled></TD>";
    html+="<TD><input type=text name=E size=2 value='"+ eq+"' onChange='MOD_STG.strat_onchangeEq("+(cl)+",this.value);'></TD>";    
    
    eqdisabled= eq ? " disabled " : ""; // if eq do not let me modify docs
    nidisabled=(IsClaimSearched(cl) || eq) ? " disabled " : ""; // if no docs, let me specify N,I
    cldisabled=(GetFeaturesLength(cl)>1 || GetFeatureUnclarity(cl,1)) ? " disabled " : "";
    
    html+="<TD><input name='C' id=C"+cl+" type=checkbox "+(IsClaimClear(cl) ?  "":"checked")
      +" onClick='MOD_STG.strat_onclickclar("+(cl)+");' "+cldisabled+"></TD>";
    html+="<TD><input name='N' id=N"+cl+" type=checkbox "+(IsClaimNotNovel(cl)    ? "checked":"")+nidisabled;
    html+=" onClick='MOD_STG.strat_onclicknov("+(cl)+");'></TD>";
    html+="<TD><input name='I' id=I"+cl+" type=checkbox "+(IsClaimNotInventive(cl)? "checked":"")+nidisabled;
    html+=" onClick='MOD_STG.strat_onclickinv("+(cl)+");'></TD>";
    if(GetDocsLength()>0) {         
      html+="<TD>"+doc_selection("DX"+cl,GetClaimDoc_X(cl),"MOD_STG.strat_onchangedoc(\"X\","+cl+")",eqdisabled)+"</TD>";
      html+="<TD>"+doc_selection("DY"+cl,GetClaimDoc_Y(cl),"MOD_STG.strat_onchangedoc(\"Y\","+cl+")",eqdisabled)+"</TD>";
      html+="<TD>"+doc_selection("DA"+cl,GetClaimDoc_A(cl),"MOD_STG.strat_onchangedoc(\"A\","+cl+")",eqdisabled)+"</TD>";
      html+="<TD>"+doc_selection("DB"+cl,GetClaimDoc_B(cl),"MOD_STG.strat_onchangedoc(\"B\","+cl+")",eqdisabled)+"</TD>";
      if(GetPrefsAttr("docC")) {
	html+="<TD>"+doc_selection("DC"+cl,GetClaimDoc_C(cl),"MOD_STG.strat_onchangedoc(\"C\","+cl+")",eqdisabled)+"</TD>";
      }
      if(GetFeaturesLength(cl)>1) {
	bdisabled= (eq || GetFeatureEq(cl,1)) ? " disabled " : "";
	html+="<TD>"+hint_selection("DH"+cl,GetClaimHint(cl),"MOD_STG.strat_onchangedoc(\"H\","+cl+")",
				     bdisabled)+"</TD>";
      } else {
	html+="<TD>"+hint_selection("DH"+cl,GetClaimHint(cl),"MOD_STG.strat_onchangedoc(\"H\","+cl+")",
				    bdisabled)+"</TD>";
      }
    }
    html+="<TD>";    
    html+=html_warning("eqwarn"+cl,GetClaimEqWarn(cl))+" ";        
    html+=html_warning("grpwarn"+cl,GetClaimGroupWarn(cl))+" ";
    html+="</TD></TR>";
    cl_iter.next();
  }

  html+="</TABLE>";
  return html;
}



function strat_editstratSR() {
  var doc_iter = new doc_iterator();

  if(doc_iter.end()) {
    alert("No documents declared yet");
    return;
  }
  var html="<TABLE border=1>";
  while(!doc_iter.end()) { 
    var cls= [],clv=[];
    cls["X"]= new List(); clv["XN"]= new List();clv["XI"]= new List();
    cls["Y"]= new List(); clv["Y"]= new List();
    cls["A"]= new List(); clv["A"]= new List();
    cls["B"]= new List();
    cls["C"]= new List();
    var doc=doc_iter.getPN();
    var fcl_iter = new filteredclaims_iterator();
    while(!fcl_iter.end()) {
       var cl= fcl_iter.getValue(); 
       var strat=GetClaimDocStrat(cl,doc); 
       if(strat) {
	 cls[strat].push(cl);
       }
       var clval=GetClaimDocVal(cl,doc);
 
       if(clval) {
	 clv[clval].push(cl);
       }       
       fcl_iter.next();
    }    
    clv["X"]=clv["XN"];
    clv["X"].push(clv["XI"].getValue());

    html+="<TR><TH>Document</TH>";
    html+="<TH>Strategy</TH><TH> Claims</TH><TH>Value</TH><TH> Claims</TH></TR>";
    html+="<TR><TD>"+doc+"</TD>";   

    html+="<TD align=center>X/Y<sub>1</sub></TD><TD>";
    html+="<input size=60 type=text value='"+cls["X"].compact()+"' onchange='MOD_STG.strat_onchangetabSR(\""+doc+"\",this.value,\"X\");'>";

    html+="<TD align=center>X</TD><TD>";
    html+="<input size=60 type=text value='"+clv["X"].compact()+"' ";
    html+="</TD></TR>";
    
    html+="<TR><TD ></TD><TD align=center>Y<sub>2</sub></TD><TD>";
    html+="<input size=60 type=text value='"+cls["Y"].compact()+"' onchange='MOD_STG.strat_onchangetabSR(\""+doc+"\",this.value,\"Y\");'>";

    html+="<TD align=center>Y</TD><TD>";
    html+="<input size=60 type=text value='"+clv["Y"].compact()+"' ";
    html+="</TD></TR>";

  
    html+="<TR><TD ></TD><TD align=center>A</TD><TD>";
    html+="<input size=60 type=text value='"+cls["A"].compact()+"' onchange='MOD_STG.strat_onchangetabSR(\""+doc+"\",this.value,\"A\");'>";
    html+="<TD align=center>A</TD><TD>";
    html+="<input size=60 type=text value='"+clv["A"].compact()+"' ";
    html+="</TD></TR>";


    html+="<TR><TD ></TD><TD align=center>B</TD><TD>";
    html+="<input size=60 type=text value='"+cls["B"].compact()+"' onchange='MOD_STG.strat_onchangetabSR(\""+doc+"\",this.value,\"B\");'>";

   if(GetPrefsAttr("docC")) {
     html+="<TR><TD ></TD><TD align=center>C</TD><TD>";
     html+="<input size=60 type=text value='"+cls["C"].compact()+"' onchange='MOD_STG.strat_onchangetabSR(\""+doc+"\",this.value,\"C\");'>";
   }   
    html+="</TD></TR>";
    
    doc_iter.next();
  } 
  html+="</TABLE>";


 
  return html;
}


MOD_STG.objtabs=[];
MOD_STG.objtabs["List"]={  callback:strat_cldoclist};
MOD_STG.objtabs["Table"]={ callback:strat_editstratSR};

function edit_strategy() {  
  InitDocPNS(); 
  InitDocPNB(); 

  if(!CLAIMSET) {
    alert("Create or Select a claim set first !");
    return;
  }
  /*
  var html="<TABLE width=100%>"; 
  html+="<TR><TH align=left width=530>"+filter_selection("edit_strategy()")+"</TH>"; 
  html+="<TH ALIGN=RIGHT width=170><fieldset><Legend>No opinion on</legend>"+html_img(notsearched_img);
  html+="<input type=text readonly class=ROTXT value='"+List_tolist(GetAllClaims("??"))+"' id='noop'></fieldset>";
  html+="</TH></TR>";
  html+="<TR><TD colspan=2 width=100%><HR>"+html_tabs_obj("stratview",MOD_STG.objtabs)+"</TD></TR>";
  html+="</TABLE>"; 
  */
  ClaimToolEditFilterSection("Edit Strategy",html_tabs_obj("stratview",MOD_STG.objtabs),"edit_strategy()");

}
